package com.example.myapplication;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.bean.Person;
import com.example.myapplication.dao.MySQLiteOpenHelper;
import com.example.myapplication.databinding.Activity2MainEditBinding;
import com.example.myapplication.service.SharedPreferencesService;

import java.util.Map;
import java.util.Objects;

public class MainActivity2Edit extends AppCompatActivity {

    private Activity2MainEditBinding EditBinding;
    private MySQLiteOpenHelper mySQLiteOpenHelper;
    private Intent intent;

    // 真有数据，假无数据
    private boolean isName = false;
    private boolean isPhone = false;
    private boolean isPhone2 = true;
    private boolean isSex = false;

    // 默认-1未选择任何关系，1已选择其他并且已备注/未选择其他，0选中其他未备注
    private int isCheck = -1;

    // 关系固定组
    boolean isCheckBoxGroup = false;

    // 回显原名
    String echoName = null;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2_main_edit);

        // 获取上个页面发出的请求数据
        intent = getIntent();
        String request = intent.getStringExtra("home");

        EditBinding = Activity2MainEditBinding.inflate(getLayoutInflater());
        setContentView(EditBinding.getRoot());

        mySQLiteOpenHelper = new MySQLiteOpenHelper(this);

        // 操作校验
        isOperate(0);
        // 复选框
        CheckBox();
        switch (request.isEmpty() ? "" : request) {
            case "insert":
                // 按钮切换
                EditBinding.addSave.setVisibility(View.VISIBLE);
                EditBinding.editSave.setVisibility(View.GONE);
                // 标题文本切换
                EditBinding.addTitle.setVisibility(View.VISIBLE);
                EditBinding.editTile.setVisibility(View.GONE);
                // 新增联系人
                EditBinding.addSave.setOnClickListener(view -> {
                    lastCheck(request);
                });
                break;
            case "update":
                // 按钮切换
                EditBinding.addSave.setVisibility(View.GONE);
                EditBinding.editSave.setVisibility(View.VISIBLE);
                // 标题文本切换
                EditBinding.addTitle.setVisibility(View.GONE);
                EditBinding.editTile.setVisibility(View.VISIBLE);
                echo();
                // 修改联系人
                EditBinding.editSave.setOnClickListener(view -> {
                    lastCheck(request);
                });
                break;
            default:
                Toast.makeText(this, "不好意思，请求的数据掉啦", Toast.LENGTH_SHORT).show();
        }
        // 取消
        EditBinding.editCancelText.setOnClickListener(view -> {
            offHint();
        });


    }

    // 取消/返回提示
    private void offHint() {
        isCheckBoxGroup = !EditBinding.editCheckbox1.isChecked() && !EditBinding.editCheckbox2.isChecked() && !EditBinding.editCheckbox3.isChecked() && !EditBinding.editCheckbox4.isChecked();
        if (isName || isPhone || isSex || !isCheckBoxGroup || !(EditBinding.editSexRadioGroup.getCheckedRadioButtonId() == -1)
                || !EditBinding.editPhone2.getText().toString().trim().isEmpty()
                || !EditBinding.editAddress.getText().toString().trim().isEmpty()) {
            LayoutInflater inflater = LayoutInflater.from(this);
            View customView = inflater.inflate(R.layout.pop_up, null);
            TextView title = customView.findViewById(R.id.up_title);
            TextView message = customView.findViewById(R.id.up_tip);
            title.setText("取消提示");
            message.setText("确认要返回吗？返回不会保留数据哦~");
//            customView.setBackground(ContextCompat.getDrawable(this,R.drawable.pop_up_1));

            // 创建一个弹窗构造器
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            /*builder.setTitle("标题"); // 设置标题
            builder.setMessage("这是弹窗信息"); // 设置信息
            builder.setPositiveButton("确定", (dialog, which) -> {
                // 点击确定按钮后的回调

                finish();
            });*/

            // 构建dialog显示的view布局
            /*View view_dialog = getLayoutInflater().from(this).inflate(R.layout.pop_up, null);
            Window window = dialog.getWindow();
            // 设置显示视图内容
            window.setContentView(view_dialog);
            WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
            lp.width = 500;// 调整该值可以设置对话框显示的宽度
            lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            window.setAttributes(lp);*/

            builder.setView(customView);
            // 创建并显示弹窗
            AlertDialog dialog = builder.create();
            dialog.show();
            /*弹窗-确认*/
            TextView sure = customView.findViewById(R.id.up_confirm);
            sure.setOnClickListener(view1 -> {
                finish();
            });
            /*弹窗-取消*/
            TextView off = customView.findViewById(R.id.up_cancel);
            off.setOnClickListener(view1 -> {
                dialog.cancel();
            });
        } else {
            intent = new Intent(this, MainActivity2PageView.class);
            startActivity(intent);
            finish();
        }
    }

    // 保存/修改确认后最终校验
    private void lastCheck(String request) {
        // 必选项判断
        if (isName && isPhone && isSex) {
            // 可选项提醒判断
            switch (isCheck) {
                case -1:
                    LayoutInflater inflater = LayoutInflater.from(this);
                    View customView = inflater.inflate(R.layout.pop_up, null);
                    TextView title = customView.findViewById(R.id.up_title);
                    TextView message = customView.findViewById(R.id.up_tip);
                    title.setText("关系提示");
                    message.setText("确定不填写关系嘛？无关系默认认定 " + EditBinding.editName.getText().toString().trim() + " 为陌生人哦~");
                    // 创建一个弹窗构造器
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setView(customView);
                    // 创建并显示弹窗
                    AlertDialog dialog = builder.create();
                    dialog.show();
                    // 弹窗-确认
                    TextView sure = customView.findViewById(R.id.up_confirm);
                    sure.setOnClickListener(view1 -> {
                        submit(request);
                    });
                    // 弹窗-取消
                    TextView off = customView.findViewById(R.id.up_cancel);
                    off.setOnClickListener(view1 -> {
                        dialog.cancel();
                    });
                    break;
                case 0:
                    EditBinding.editRelationTip.setTextColor(ContextCompat.getColor(MainActivity2Edit.this, R.color.red));
                    EditBinding.editRelationTip.setText("选其他需备注");
                    Toast.makeText(this, "填写不完整", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    submit(request);
            }
        } else {
            isOperate(1);
            Toast.makeText(this, "填写不完整", Toast.LENGTH_SHORT).show();
        }
    }

    // 确认提交--开始保存数据
    private void submit(String request) {
        if (Objects.equals(request, "insert")) {
            Person person = getPersonInfo();
            boolean isSuccess = mySQLiteOpenHelper.insertPerson(person);
            if (isSuccess) {
                Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
                intent = new Intent(this, MainActivity2PageView.class);
                intent.putExtra("return", request);
                startActivity(intent);
                finish();
            } else {
                Toast.makeText(this, "添加失败", Toast.LENGTH_SHORT).show();
            }
        } else if (request.equals("update")) {
            boolean isSuccess = mySQLiteOpenHelper.updatePerson(getPersonInfo(), echoName);
            if (isSuccess) {
                Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
                intent = new Intent(this, MainActivity2PageView.class);
                intent.putExtra("return", request);
                startActivity(intent);
                finish();
            } else {
                Toast.makeText(this, "修改失败", Toast.LENGTH_SHORT).show();
            }
        }
    }

    // 获取用户输入的数据
    private Person getPersonInfo() {
        Person person = new Person();
        String name = EditBinding.editName.getText().toString().trim();

        // 获取单选信息
        int radioButtonID = EditBinding.editSexRadioGroup.getCheckedRadioButtonId();
        RadioButton radioButton = findViewById(radioButtonID);
        String sex = radioButton.getText().toString().trim();

        String phone = EditBinding.editPhone.getText().toString().trim();
        String phone2 = EditBinding.editPhone2.getText().toString().trim();

        String relation = "";
        // 1家人，2朋友，3师生，4同事，5#其他(备注)，-1陌生人(未定义)
        boolean isbox = false;
        if (EditBinding.editCheckbox1.isChecked()) {
            relation = "1";
        } else if (EditBinding.editCheckbox2.isChecked()) {
            relation = "2";
        } else if (EditBinding.editCheckbox3.isChecked()) {
            relation = "3";
        } else if (EditBinding.editCheckbox4.isChecked()) {
            relation = "4";
        } else {
            isbox = true;
        }
        String reText = null;
        if (isbox) {
            if (EditBinding.editCheckbox5.isChecked()) {
                relation = "5";
                reText = EditBinding.editRelationRemark.getText().toString().trim();
            } else {
                relation = "-1";
            }
        } else {
            if (EditBinding.editCheckbox5.isChecked()) {
                relation += "_5";
                reText = EditBinding.editRelationRemark.getText().toString().trim();
            }
        }

        String address = EditBinding.editAddress.getText().toString().trim();

        Integer userId = (Integer) SharedPreferencesService.getUserInfo(this).get("userId");

        person.setName(name);
        person.setSex("男".equals(sex) ? 1 : 0);
        person.setPhone(phone);
        person.setPhone2(phone2);
        person.setRelation(relation);
        person.setReText(reText);
        person.setAddress(address);
        person.setUserId(userId);
        return person;
    }

    /**
     * 用户输入操作事件监听校验
     *
     * @param i 保存开关：1开0关
     */
    private void isOperate(int i) {
        EditBinding.editNameTip.setTextColor(ContextCompat.getColor(MainActivity2Edit.this, R.color.red));
        EditBinding.editPhoneTip.setTextColor(ContextCompat.getColor(MainActivity2Edit.this, R.color.red));
        EditBinding.editPhone2Tip.setTextColor(ContextCompat.getColor(MainActivity2Edit.this, R.color.red));
        EditBinding.editSexTip.setTextColor(ContextCompat.getColor(MainActivity2Edit.this, R.color.red));
        if (i > 0 && !isName) {
            EditBinding.editNameTip.setText("姓名不能为空");
        }
        if (i > 0 && !isPhone) {
            EditBinding.editPhoneTip.setText("手机不能为空");
        }

        // 姓名校验
        EditBinding.editName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // 文本改变之前的处理
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 文本正在改变时的处理
            }

            @Override
            public void afterTextChanged(Editable s) {
                // 文本改变之后的处理
                if (EditBinding.editName.getText().toString().trim().isEmpty()) {
                    EditBinding.editNameTip.setText("姓名不能为空");
                    isName = false;
                } else {
                    EditBinding.editNameTip.setText("");
                    isName = true;
                }
                relationOtherTip();
                // 这里可以处理输入的字符，例如检查输入内容
            }
        });
        // 手机校验
        EditBinding.editPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // 文本改变之前的处理
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 文本正在改变时的处理
            }

            @Override
            public void afterTextChanged(Editable s) {
                // 文本改变之后的处理
                if (EditBinding.editPhone.getText().toString().trim().isEmpty()) {
                    EditBinding.editPhoneTip.setText("手机不能为空");
                    isPhone = false;
                } else if (EditBinding.editPhone.getText().toString().trim().length() > 11) {
                    EditBinding.editPhoneTip.setText("手机号不合法");
                    isPhone = false;
                } else {
                    EditBinding.editPhoneTip.setText("");
                    isPhone = true;
                }
                // 这里可以处理输入的字符，例如检查输入内容
            }
        });
        // 手机2校验
        EditBinding.editPhone2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // 文本改变之前的处理
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 文本正在改变时的处理
            }

            @Override
            public void afterTextChanged(Editable s) {
                // 文本改变之后的处理
                if (EditBinding.editPhone2.getText().toString().trim().isEmpty()) {
                    EditBinding.editPhoneTip.setText("");
                    isPhone2 = true;
                } else if (EditBinding.editPhone2.getText().toString().trim().length() > 11) {
                    EditBinding.editPhoneTip.setText("手机号不合法");
                    isPhone2 = false;
                } else {
                    EditBinding.editPhoneTip.setText("");
                    isPhone2 = true;
                }
                // 这里可以处理输入的字符，例如检查输入内容
            }
        });
        // 性别校验
        if (i > 0 && EditBinding.editSexRadioGroup.getCheckedRadioButtonId() == -1) {
            EditBinding.editSexTip.setText("性别不可忽略");
            isSex = false;
        }
        EditBinding.editSexRadioGroup.setOnCheckedChangeListener((radioGroup, i1) -> {
            EditBinding.editSexTip.setText("");
            isSex = true;
        });
    }

    // 关系--其他--提醒
    private void relationOtherTip() {
        isCheckBoxGroup = !EditBinding.editCheckbox1.isChecked() && !EditBinding.editCheckbox2.isChecked() && !EditBinding.editCheckbox3.isChecked() && !EditBinding.editCheckbox4.isChecked();
        if (EditBinding.editCheckbox5.isChecked()) {
            if (EditBinding.editRelationRemark.getText().toString().trim().isEmpty()) {
                EditBinding.editRelationTip.setTextColor(ContextCompat.getColor(MainActivity2Edit.this, R.color.red));
                EditBinding.editRelationTip.setText("选其他需备注");
                isCheck = 0;
            } else {
                EditBinding.editRelationTip.setText("");
                isCheck = 1;
            }
        } else {
            if (isCheckBoxGroup && isName) {
                EditBinding.editRelationTip.setTextColor(ContextCompat.getColor(MainActivity2Edit.this, R.color.dark_yellow));
                EditBinding.editRelationTip.setText("关系未能明确");
                isCheck = -1;
            } else {
                EditBinding.editRelationTip.setText("");
                isCheck = 1;
            }
        }
    }

    // 复选框触发事件
    private void CheckBox() {
        //获取复选信息（最多只能选两个）
        EditBinding.editRelationRemark.setEnabled(false);
        EditBinding.editRelationRemark.setHint("其他备注项");
        EditBinding.editRelationRemark.setBackgroundColor(Color.TRANSPARENT);
        // 四个复选绑定一组
        EditBinding.editCheckbox1.setOnClickListener(view -> {
            if (EditBinding.editCheckbox1.isChecked()) {
                EditBinding.editCheckbox2.setEnabled(false);
                EditBinding.editCheckbox2.setTextColor(ContextCompat.getColor(this, R.color.gray));
                EditBinding.editCheckbox2.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#999999")));
                EditBinding.editCheckbox3.setEnabled(false);
                EditBinding.editCheckbox3.setTextColor(ContextCompat.getColor(this, R.color.gray));
                EditBinding.editCheckbox3.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#999999")));
                EditBinding.editCheckbox4.setEnabled(false);
                EditBinding.editCheckbox4.setTextColor(ContextCompat.getColor(this, R.color.gray));
                EditBinding.editCheckbox4.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#999999")));
                EditBinding.editRelationTip.setText("");
            } else {
                EditBinding.editCheckbox2.setEnabled(true);
                EditBinding.editCheckbox2.setTextColor(ContextCompat.getColor(this, R.color.dark_gray));
                EditBinding.editCheckbox2.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#CC33FF")));
                EditBinding.editCheckbox3.setEnabled(true);
                EditBinding.editCheckbox3.setTextColor(ContextCompat.getColor(this, R.color.dark_gray));
                EditBinding.editCheckbox3.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#CC33FF")));
                EditBinding.editCheckbox4.setEnabled(true);
                EditBinding.editCheckbox4.setTextColor(ContextCompat.getColor(this, R.color.dark_gray));
                EditBinding.editCheckbox4.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#CC33FF")));
            }
            relationOtherTip();
        });
        EditBinding.editCheckbox2.setOnClickListener(view -> {
            if (EditBinding.editCheckbox2.isChecked()) {
                EditBinding.editCheckbox1.setEnabled(false);
                EditBinding.editCheckbox1.setTextColor(ContextCompat.getColor(this, R.color.gray));
                EditBinding.editCheckbox1.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#999999")));
                EditBinding.editCheckbox3.setEnabled(false);
                EditBinding.editCheckbox3.setTextColor(ContextCompat.getColor(this, R.color.gray));
                EditBinding.editCheckbox3.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#999999")));
                EditBinding.editCheckbox4.setEnabled(false);
                EditBinding.editCheckbox4.setTextColor(ContextCompat.getColor(this, R.color.gray));
                EditBinding.editCheckbox4.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#999999")));
                EditBinding.editRelationTip.setText("");
            } else {
                EditBinding.editCheckbox1.setEnabled(true);
                EditBinding.editCheckbox1.setTextColor(ContextCompat.getColor(this, R.color.dark_gray));
                EditBinding.editCheckbox1.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#CC33FF")));
                EditBinding.editCheckbox3.setEnabled(true);
                EditBinding.editCheckbox3.setTextColor(ContextCompat.getColor(this, R.color.dark_gray));
                EditBinding.editCheckbox3.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#CC33FF")));
                EditBinding.editCheckbox4.setEnabled(true);
                EditBinding.editCheckbox4.setTextColor(ContextCompat.getColor(this, R.color.dark_gray));
                EditBinding.editCheckbox4.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#CC33FF")));
            }
            relationOtherTip();
        });
        EditBinding.editCheckbox3.setOnClickListener(view -> {
            if (EditBinding.editCheckbox3.isChecked()) {
                EditBinding.editCheckbox1.setEnabled(false);
                EditBinding.editCheckbox1.setTextColor(ContextCompat.getColor(this, R.color.gray));
                EditBinding.editCheckbox1.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#999999")));
                EditBinding.editCheckbox2.setEnabled(false);
                EditBinding.editCheckbox2.setTextColor(ContextCompat.getColor(this, R.color.gray));
                EditBinding.editCheckbox2.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#999999")));
                EditBinding.editCheckbox4.setEnabled(false);
                EditBinding.editCheckbox4.setTextColor(ContextCompat.getColor(this, R.color.gray));
                EditBinding.editCheckbox4.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#999999")));
                EditBinding.editRelationTip.setText("");
            } else {
                EditBinding.editCheckbox1.setEnabled(true);
                EditBinding.editCheckbox1.setTextColor(ContextCompat.getColor(this, R.color.dark_gray));
                EditBinding.editCheckbox1.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#CC33FF")));
                EditBinding.editCheckbox2.setEnabled(true);
                EditBinding.editCheckbox2.setTextColor(ContextCompat.getColor(this, R.color.dark_gray));
                EditBinding.editCheckbox2.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#CC33FF")));
                EditBinding.editCheckbox4.setEnabled(true);
                EditBinding.editCheckbox4.setTextColor(ContextCompat.getColor(this, R.color.dark_gray));
                EditBinding.editCheckbox4.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#CC33FF")));
            }
            relationOtherTip();
        });
        EditBinding.editCheckbox4.setOnClickListener(view -> {
            if (EditBinding.editCheckbox4.isChecked()) {
                EditBinding.editCheckbox1.setEnabled(false);
                EditBinding.editCheckbox1.setTextColor(ContextCompat.getColor(this, R.color.gray));
                EditBinding.editCheckbox1.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#999999")));
                EditBinding.editCheckbox2.setEnabled(false);
                EditBinding.editCheckbox2.setTextColor(ContextCompat.getColor(this, R.color.gray));
                EditBinding.editCheckbox2.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#999999")));
                EditBinding.editCheckbox3.setEnabled(false);
                EditBinding.editCheckbox3.setTextColor(ContextCompat.getColor(this, R.color.gray));
                EditBinding.editCheckbox3.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#999999")));
                EditBinding.editRelationTip.setText("");
            } else {
                EditBinding.editCheckbox1.setEnabled(true);
                EditBinding.editCheckbox1.setTextColor(ContextCompat.getColor(this, R.color.dark_gray));
                EditBinding.editCheckbox1.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#CC33FF")));
                EditBinding.editCheckbox2.setEnabled(true);
                EditBinding.editCheckbox2.setTextColor(ContextCompat.getColor(this, R.color.dark_gray));
                EditBinding.editCheckbox2.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#CC33FF")));
                EditBinding.editCheckbox3.setEnabled(true);
                EditBinding.editCheckbox3.setTextColor(ContextCompat.getColor(this, R.color.dark_gray));
                EditBinding.editCheckbox3.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#CC33FF")));
            }
            relationOtherTip();
        });

        // 可选按钮选中需必填
        EditBinding.editCheckbox5.setOnClickListener(view -> {
            if (EditBinding.editCheckbox5.isChecked()) {
                EditBinding.editRelationRemark.setEnabled(true);
                EditBinding.editRelationRemark.setHint("必填");
                EditBinding.editRelationRemark.setBackground(ContextCompat.getDrawable(this, R.drawable.text_2));
                EditBinding.editRelationTip.setText("");
                isCheck = 0;
            } else {
                EditBinding.editRelationRemark.setText("");
                EditBinding.editRelationRemark.setEnabled(false);
                EditBinding.editRelationRemark.setHint("其他备注项");
                EditBinding.editRelationRemark.setBackgroundColor(Color.TRANSPARENT);
                relationOtherTip();
            }
        });
        EditBinding.editRelationRemark.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                EditBinding.editRelationTip.setTextColor(ContextCompat.getColor(MainActivity2Edit.this, R.color.red));
                relationOtherTip();
            }
        });
    }

    // 数据回显
    private void echo() {
        Person person = getPersonBean();
        echoName = person.getName();
        EditBinding.editName.setText(echoName);
        if (person.getSex() == 1) {
            EditBinding.sexManRadioBtn.setChecked(true);
        } else {
            EditBinding.sexWomanRadioBtn.setChecked(true);
        }
        EditBinding.editPhone.setText(person.getPhone());
        EditBinding.editPhone2.setText(person.getPhone2());
        String str = person.getRelation();
        int[] arr = new int[2];

        if (str.contains("_")) {
            arr = convertToIntArray(str.split("_"));
        } else {
            arr[0] = Integer.parseInt(str);
        }
        // 1家人，2朋友，3师生，4同事，5#其他(备注)，-1陌生人(未定义)
        for (int i : arr) {
            switch (i) {
                case 1:
                    EditBinding.editCheckbox1.setChecked(true);
                    EditBinding.editCheckbox2.setEnabled(false);
                    EditBinding.editCheckbox2.setTextColor(ContextCompat.getColor(this, R.color.gray));
                    EditBinding.editCheckbox2.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#999999")));
                    EditBinding.editCheckbox3.setEnabled(false);
                    EditBinding.editCheckbox3.setTextColor(ContextCompat.getColor(this, R.color.gray));
                    EditBinding.editCheckbox3.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#999999")));
                    EditBinding.editCheckbox4.setEnabled(false);
                    EditBinding.editCheckbox4.setTextColor(ContextCompat.getColor(this, R.color.gray));
                    EditBinding.editCheckbox4.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#999999")));
                    EditBinding.editRelationTip.setText("");
                    break;
                case 2:
                    EditBinding.editCheckbox2.setChecked(true);
                    EditBinding.editCheckbox1.setEnabled(false);
                    EditBinding.editCheckbox1.setTextColor(ContextCompat.getColor(this, R.color.gray));
                    EditBinding.editCheckbox1.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#999999")));
                    EditBinding.editCheckbox3.setEnabled(false);
                    EditBinding.editCheckbox3.setTextColor(ContextCompat.getColor(this, R.color.gray));
                    EditBinding.editCheckbox3.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#999999")));
                    EditBinding.editCheckbox4.setEnabled(false);
                    EditBinding.editCheckbox4.setTextColor(ContextCompat.getColor(this, R.color.gray));
                    EditBinding.editCheckbox4.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#999999")));
                    EditBinding.editRelationTip.setText("");
                    break;
                case 3:
                    EditBinding.editCheckbox3.setChecked(true);
                    EditBinding.editCheckbox2.setEnabled(false);
                    EditBinding.editCheckbox2.setTextColor(ContextCompat.getColor(this, R.color.gray));
                    EditBinding.editCheckbox2.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#999999")));
                    EditBinding.editCheckbox1.setEnabled(false);
                    EditBinding.editCheckbox1.setTextColor(ContextCompat.getColor(this, R.color.gray));
                    EditBinding.editCheckbox1.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#999999")));
                    EditBinding.editCheckbox4.setEnabled(false);
                    EditBinding.editCheckbox4.setTextColor(ContextCompat.getColor(this, R.color.gray));
                    EditBinding.editCheckbox4.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#999999")));
                    EditBinding.editRelationTip.setText("");
                    break;
                case 4:
                    EditBinding.editCheckbox4.setChecked(true);
                    EditBinding.editCheckbox2.setEnabled(false);
                    EditBinding.editCheckbox2.setTextColor(ContextCompat.getColor(this, R.color.gray));
                    EditBinding.editCheckbox2.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#999999")));
                    EditBinding.editCheckbox3.setEnabled(false);
                    EditBinding.editCheckbox3.setTextColor(ContextCompat.getColor(this, R.color.gray));
                    EditBinding.editCheckbox3.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#999999")));
                    EditBinding.editCheckbox1.setEnabled(false);
                    EditBinding.editCheckbox1.setTextColor(ContextCompat.getColor(this, R.color.gray));
                    EditBinding.editCheckbox1.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#999999")));
                    EditBinding.editRelationTip.setText("");
                    break;
                case 5:
                    EditBinding.editCheckbox5.setChecked(true);
                    EditBinding.editRelationRemark.setText(person.getReText());
                    EditBinding.editRelationRemark.setEnabled(true);
                    EditBinding.editRelationRemark.setBackground(ContextCompat.getDrawable(this, R.drawable.text_2));
                    break;
                default:
            }
        }
        EditBinding.editAddress.setText(person.getAddress());
        //echoVerify(person);
        if (!person.getRelation().isEmpty()) {
            if (person.getRelation().equals("-1")) {
                isCheck = -1;
            } else {
                isCheck = 1;
            }
        }
    }

    // 回显校验
    private void echoVerify(Person person) {
        isName = !person.getName().isEmpty();
        isSex = person.getSex() == 1 || person.getSex() == 0;
        isPhone = !person.getPhone().isEmpty();
        isPhone2 = !person.getPhone2().isEmpty();
        if (!person.getRelation().isEmpty()) {
            if (person.getRelation().equals("-1")) {
                isCheck = -1;
            } else {
                isCheck = 1;
            }
        }
    }

    // 从Intent中获取并封装Person对象
    private Person getPersonBean() {
        Intent intent = getIntent();

        String name = intent.getStringExtra("name");
        int sex = intent.getIntExtra("sex", -1);
        String phone = intent.getStringExtra("phone");
        String phone2 = intent.getStringExtra("phone2");
        String relation = intent.getStringExtra("relation");
        String reText = intent.getStringExtra("reText");
        String address = intent.getStringExtra("address");

        Person person = new Person();
        person.setName(name);
        person.setSex(sex);
        person.setPhone(phone);
        person.setPhone2(phone2);
        person.setRelation(relation);
        person.setReText(reText);
        person.setAddress(address);

        return person;
    }

    /**
     * 转 int[]类型
     *
     * @param objArray
     * @return
     */
    private static int[] convertToIntArray(String[] objArray) {
        int[] intArray = new int[objArray.length];
        for (int i = 0; i < objArray.length; i++) {
            // 强制类型转换，从Object到Integer
            intArray[i] = Integer.parseInt(objArray[i]);
        }
        return intArray;
    }

    // 用来处理通过系统返回的操作
    @Override
    public void onBackPressed() {
        offHint();
    }
}